package com.frxuexi.park2303

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.base.mvi.BaseViewActivity
import com.frxuexi.park2303.databinding.ActivitySplashBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Timer
import kotlin.concurrent.timerTask

class SplashActivity : BaseViewActivity<ActivitySplashBinding>() {

    var timer:Timer?=null
//    lateinit var timer: Timer
    var num=5
    override fun getBind(): ActivitySplashBinding {
        return ActivitySplashBinding.inflate(layoutInflater)
    }

    override fun initView() {
        val time = SPUtils.getInstance().getLong("time", 0)
        val diff=(System.currentTimeMillis()-time)/1000/60/24
        if(diff<7){
            start()
        }
        lifecycleScope.launch(Dispatchers.IO) {
            timer=Timer()
            timer!!.schedule(timerTask {
                lifecycleScope.launch(Dispatchers.Main) {
                    binding.splashTime.setText("${num}s")
                }
                if(num==0){
                    start()
                }
                num--

            },0,1000)
        }


        binding.splashSkip.setOnClickListener {
            start()
        }
    }

    fun start(){
        SPUtils.getInstance().put("time",System.currentTimeMillis())
        timer?.cancel()
        ARouter.getInstance().build("/app/LoginActivity").navigation()
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        if(timer!=null){
            timer!!.cancel()
            timer=null
        }
    }

}